package com.qiudaozhang.jblog.article.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiudaozhang.jblog.article.mapper.ArticleMapper;
import com.qiudaozhang.jblog.article.service.ArticleService;
import com.qiudaozhang.jblog.article.service.ArticleTagService;
import com.qiudaozhang.jblog.model.Article;
import com.qiudaozhang.jblog.model.ArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service(version = "${service.version}",group = "${dubbo.group}")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;


    @Autowired
    private ArticleTagService articleTagService;

    @Override
    public Article getById(Integer id) {
//        return articleMapper.getById(id);
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public ArticleInfo getDetail(Integer id) {
        return   articleMapper.getDetail(id);
    }

    @Override
    public Integer save(ArticleInfo article) {
        handleDescription(article);
        articleMapper.insertReturnPrimaryKey(article);
        // 存储article_tag中
        articleTagService.save(article.getArticleId(),article.getTagIds());
        // 处理关联的标签
        return article.getArticleId();
    }

    /**
     * 处理文章简介
     *
     * @param article
     */
    private void handleDescription(ArticleInfo article) {
        int len = 200;
        if (article.getContent().length() <= 200) {
            len = article.getContent().length();
        }
        String temp = article.getContent().substring(0, len);
        temp = temp.replace("#", "");
        temp = temp.replace("```", "");
        article.setDescription(temp);


    }

    @Override
    public Integer update(Article article) {
        return null;
    }

    @Override
    public void delete(Integer id) {
        articleMapper.deleteByPrimaryKey(id);
        // 删除中间表关联的数据
        articleTagService.deleteByArticleId(id);
    }


    @Override
    public PageInfo<Article> queryPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> l = articleMapper.selectAll();
        PageInfo<Article> p = new PageInfo<>(l);
        return p;
    }

    @Override
    public PageInfo<ArticleInfo> queryPageOrderByUpdateTimeDesc(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleInfo> l = articleMapper.selectAllOrderByUpdateTimeDesc();
        PageInfo<ArticleInfo> p = new PageInfo<>(l);
        return p;
    }


    @Override
    public void updateCategoryNull(Integer categoryId) {
        articleMapper.updateCategoryNull(categoryId);

    }

    @Override
    public void updateDetail(ArticleInfo articleInfo) {
//        Article article = articleMapper.selectByPrimaryKey(articleInfo.getArticleId());
        // 主要更新标题，内容，其它不动。
        articleInfo.setUpdateTime(new Date());
        articleMapper.update(articleInfo);
        //更新标签，先删除所有的本文章的article关联的ID,然后重新添加所有的关联的ID。
        articleTagService.deleteByArticleId(articleInfo.getArticleId());
        // 重新添加
        articleTagService.save(articleInfo.getArticleId(),articleInfo.getTagIds());
    }

    @Override
    public PageInfo<ArticleInfo> queryPageOrderBySearch(String search,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleInfo> l = articleMapper.queryPageOrderBySearch(search);
        PageInfo<ArticleInfo> p = new PageInfo<>(l);
        return p;
    }


}
